package com.hx.myfristandriod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class TestListViewActivity extends Activity {
    
	    private ListView myListView;
	    private List<Map<String, ?>> data;
	    @Override
		protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.listview_test);
	        this.myListView=(ListView) findViewById(R.id.myListView);
	        //序列化数据
	        String message="1/我的标题/2014-05-29#2/我的标题2/2014-05-30#3/我的标题3/2014-05-31";
	        this.data=getData(message);
	        //創建適配器，綁定數據
	        SimpleAdapter adapter=new SimpleAdapter(this, data,android.R.layout.simple_list_item_2, new String[]{"title","date"}, new int[]{android.R.id.text1,android.R.id.text2});
	        //将适配器绑定组件
	        myListView.setAdapter(adapter);
	        //将监听绑定到组件
	        myListView.setOnItemClickListener(new ListViewOcl());
	    }

	private List<Map<String, ?>> getData(String msg){
		List<Map<String, ?>> listData=new ArrayList<Map<String,?>>();
		
		String []items=msg.split("#");
		for(int i=0;i<items.length;i++){
			String []content=items[i].split("/");
			  Map<String, String> item=new HashMap<String, String>();
			  item.put("id", content[0]);
			  item.put("title", content[1]);
			  item.put("date", content[2]);
			  listData.add(item);
		  }
		return listData;
	}
  
	class ListViewOcl implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int position,
				long id) {
		Map<String, ?> item=data.get(position);
		Toast.makeText(getApplicationContext(), "您选择了编号为："+item.get("id")+"标题是："+item.get("title"), Toast.LENGTH_LONG).show();
			
		}
		
		
	}
}
